{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 标准库 pathlib"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 简单操作"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-04T14:58:23.180315Z",
     "start_time": "2024-07-04T14:58:23.168711Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前文件路径是：/Users/ranhongliang/Documents/workspace/PythonProgram/std_lib\n",
      "列出所有文件：\n",
      "[PosixPath('README_ORIGIN.md'), PosixPath('readme.md'), PosixPath('std_pathlib.ipynb')]\n",
      "列出当前目录树下的所有 Python jumper 源代码文件:"
     ]
    },
    {
     "data": {
      "text/plain": "[PosixPath('std_pathlib.ipynb')]"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "p = Path(\".\")\n",
    "\n",
    "print(f\"当前文件路径是：{p.absolute()}\")\n",
    "\n",
    "print('列出所有文件：')\n",
    "filename = [x for x in p.iterdir() if x.is_file()]\n",
    "print(filename)\n",
    "\n",
    "print(\"列出当前目录树下的所有 Python jumper 源代码文件:\", end=\"\")\n",
    "list(p.glob(\"**/*.ipynb\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1. 创建pathlib对象"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/Users/ranhongliang/Documents/workspace/PythonProgram/std_lib\n",
      "/path/to/your/directory\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "# 创建文件对象\n",
    "file_path = Path('/etc/passwd')\n",
    "directory_path = Path(\"/etc\")\n",
    "print(file_path)\n",
    "print(directory_path)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-04T15:08:48.948534Z",
     "start_time": "2024-07-04T15:08:48.926337Z"
    }
   },
   "execution_count": 2
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2. 检查路径是否存在"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "file_path = Path('/etc/passwd')\n",
    "directory_path = Path(\"/etc\")\n",
    "\n",
    "# 是否是文件\n",
    "print(file_path.is_file())\n",
    "# 是否是目录\n",
    "print(directory_path.is_dir())\n",
    "# 是否存在\n",
    "print(directory_path.exists())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-04T15:13:04.980327Z",
     "start_time": "2024-07-04T15:13:04.972854Z"
    }
   },
   "execution_count": 4
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3. 文件操作"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/tmp/test/test.txt\n",
      "/tmp/test/test.txt 的大小是 435759 字节\n",
      "/tmp/test/test.txt 的修改时间是 2024-07-04 23:49:20.475105\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "from datetime import datetime\n",
    "\n",
    "file_path = Path('/tmp/test/test.txt')\n",
    "\n",
    "# file_path.parent 表示的是路径 /tmp/test/test.txt 的父目录路径，即 /tmp/test\n",
    "file_dir = file_path.parent\n",
    "\n",
    "# 创建目录\n",
    "# 使用file_dir.mkdir()方法，参数parents=True表示如果父目录不存在，也会一并创建\n",
    "# 参数exist_ok=True表示如果目录已存在，则不会抛出异常\n",
    "file_dir.mkdir(parents=True, exist_ok=True)\n",
    "\n",
    "# 创建文件\n",
    "file_path.touch(mode=0o755, exist_ok=True)\n",
    "\n",
    "\n",
    "# 修改权限  \n",
    "file_path.chmod(0o755)\n",
    "file_dir.chmod(0o755)\n",
    "\n",
    "# 查看当前路径\n",
    "file_path.cwd()\n",
    "\n",
    "# 移动/重命名\n",
    "# file_path.rename(Path(\"/tmp\") / 'test1.txt')\n",
    "# file_path.rename('test1.txt')\n",
    "\n",
    "# 遍历目录\n",
    "for x in file_dir.iterdir():\n",
    "    print(x)\n",
    "\n",
    "# 删除\n",
    "# file_path.unlink()\n",
    "# file_dir.rmdir()\n",
    "\n",
    "# 获取文件信息\n",
    "file_size = file_path.stat().st_size  # 获取文件大小（字节数）\n",
    "print(f\"{file_path} 的大小是 {file_size} 字节\")\n",
    "\n",
    "\n",
    "# 获取文件修改时间\n",
    "modification_time = file_path.stat().st_mtime  # 获取修改时间戳\n",
    "modification_time = datetime.fromtimestamp(modification_time)  # 转换为日期时间对象\n",
    "print(f\"{file_path} 的修改时间是 {modification_time}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-04T15:49:20.479602Z",
     "start_time": "2024-07-04T15:49:20.467087Z"
    }
   },
   "execution_count": 31
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
